﻿using System.Web.Mvc;
using System.Web.Routing;
using ASPItalia.ModelVirtualCasting.Web.Utils;
using System.Web.DynamicData;
using ASPItalia.ModelVirtualCasting.EntityFramework;
using ASPItalia.ModelVirtualCasting.BackOffice.ObjectModel;

namespace ASPItalia.ModelVirtualCasting.Web
{
    public class Global : System.Web.HttpApplication
    {
		/* Dynamica Data */
		private static MetaModel s_defaultModel = new MetaModel();
		public static MetaModel DefaultModel
		{
			get
			{
				return s_defaultModel;
			}
		}

        public static void RegisterRoutes(RouteCollection routes)
        {
			/* WebForms */
			routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");
			routes.IgnoreRoute("{myWebServices}.asmx/{*pathInfo}");
			routes.IgnoreRoute("Contents/{*pathInfo}");
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			/*Dynamic Data*/
			DefaultModel.RegisterContext(typeof(ModelVirtualCastingBackOfficeEntities), new ContextConfiguration() { ScaffoldAllTables = true });

			routes.Add(new DynamicDataRoute("admin/{table}/{action}.aspx")
			{
				Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
				Model = DefaultModel
			});
            
			/* MVC */
            #region candidate routing
            routes.MapRoute(
                "Profiles",
                "Candidate/{action}/{nickname}",                           // URL with parameters
                new { controller = "Candidate", Action = "Profile", nickname = "" }
            );
            #endregion

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

        #region customViewEngine
        private void setCustomViewEngine()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new SkinnableViewEngine());
        }
        #endregion
        #region Custom factory registration
        private void setCustomFactory()
        {
            ControllerBuilder.Current.SetControllerFactory(typeof(DynamicControllerFactory));
        }
        #endregion
    }
}